package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.supports.PageData;
import com.zjcode.cdcer.model.ZjcodeSelfTestInfo;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.ZjcodeSelfTestInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @createTime: 2023-09-15
 * @功能：风险因素表
 * @creater: yong
 */
@RestController
@Slf4j
@RequestMapping("/v1/riskfactor/")
public class TkmCerRiskFactorController extends BaseController {

    @Autowired
    private ZjcodeSelfTestInfoService zjcodeSelfTestInfoService;

    @Value("${studio.informed-url}")
    private String informedUrl;


    @PostMapping("/listAllData")
    @ApiOperation("获取所有的风险因素表数据")
    public ListResult<Object> listAllData(@RequestBody PageData pageData) {
        return ResultBuilder.buildListSuccess(zjcodeSelfTestInfoService.listAllData(pageData));
    }

    @PostMapping("getContentById")
    @ApiOperation("获取风险因素表原始数据")
    public JsonObj getContentById(@RequestBody PageData pageData) {
        String id = (String) pageData.get("id");
        ZjcodeSelfTestInfo data1 = zjcodeSelfTestInfoService.queryById(id);
        HashMap<String, Object> data = new HashMap<>();
        data.put("id", data1.getId());
        data.put("content", data1.getContent());
        data.put("colorectalCancerContent", data1.getColorectalCancerContent());
        data.put("highRisk", data1.getHighRisk());
        data.put("highRiskRemark", data1.getHighRiskRemark());
        data.put("informedConsent", StringUtils.isNotEmpty(data1.getInformedConsent()) ? informedUrl + data1.getInformedConsent() : "");
        return JsonObj.success(data);
    }

    /**
     * PC端新增风险因素自测表
     */
    @PostMapping("/edit")
    public JsonObj add(@RequestBody ZjcodeSelfTestInfo zjcodeSelfTestInfo){
        return zjcodeSelfTestInfoService.insert(zjcodeSelfTestInfo, getPageData());
    }

    /**
     * PC端修改风险因素自测表
     */
    @Deprecated
    @PostMapping("/update")
    public JsonObj update(@RequestBody ZjcodeSelfTestInfo zjcodeSelfTestInfo) {
        return zjcodeSelfTestInfoService.update(zjcodeSelfTestInfo);
    }


    @PostMapping("exportSelfTestTable")
    @ApiOperation("导出自测表信息")
    public void exportSelfTestTable( HttpServletResponse response) {
        PageData pageData = getPageData();
        zjcodeSelfTestInfoService.exportSelfTestTable(pageData,response);
    }

    // 导入自测表信息
    @PostMapping("importSelfTestTable")
    @ApiOperation("导入自测表信息")
    public JsonObj importSelfTestTable(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        PageData pageData = getPageData();
        String userId = (String) pageData.get("user");
        return zjcodeSelfTestInfoService.importSelfTestTable(file,response,userId);
    }


    /**
     *  批量导入知情同意书
     *  知情同意书命名规则：身份证号码+pdf / (jpg/png)
     * @param file
     * @param
     */
    @PostMapping("importAgreement")
    public JsonObj importSelfTestAgreement(@RequestParam("file") MultipartFile file){
        zjcodeSelfTestInfoService.importSelfTestAgreement(file);
        return JsonObj.success("导入成功");
    }
}
